package com.example.tests;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.example.utils.SortedListOf;
//import static com.example.tests.GroupDataGenerator.loadGroupsFromCsvFile;
import static com.example.tests.GroupDataGenerator.loadGroupsFromXmlFile;

public class GroupCreationTests extends TestBase {
	
	@DataProvider
	public Iterator<Object[]> groupsFromFile() throws IOException  {
		//return wrapGroupsForDataProvider(loadGroupsFromCsvFile(new File ("groups.txt"))).iterator();
		return wrapGroupsForDataProvider(loadGroupsFromXmlFile(new File ("groups.xml"))).iterator();
	}
			
  @Test(dataProvider = /*"groupsFromFile"*/ "randomValidGroupGenerator")
  public void testGroupCreationWithValidData(GroupData group) throws Exception {
	  
	// model
	SortedListOf<GroupData>  groupListFromModel = app.getModel().getGroups();
	
	// Old state from DB
	SortedListOf<GroupData> oldGroupListFromDB = app.getGroupHelper().getGroupsFromCache();
	
	// perform actions
	app.getGroupHelper().createGroup(group);
    
    // New state from UI
	SortedListOf<GroupData>  newGroupListFromUI = app.getGroupHelper().getUiGroups();
	
	// New state from DB
	SortedListOf<GroupData>  newGroupListFromDB = app.getGroupHelper().getGroupsFromCache();
	
    // compare states
    assertThat(newGroupListFromUI, equalTo(groupListFromModel));
    assertThat(newGroupListFromDB, equalTo(groupListFromModel));
    assertThat(newGroupListFromUI, equalTo(newGroupListFromDB));
    assertThat(newGroupListFromDB, equalTo(oldGroupListFromDB.withAdded(group)));
    
    // ============================================================
 /*   // compare model to implementation
    if (wantToCheck()) {
    	if ("yes".equals(app.getProperty("chek.db"))) {
    		assertThat(app.getModel().getGroups(), equalTo(app.getHibernateHelper().listGroups()));
    	}
     
    if ("yes".equals(app.getProperty("chek.ui"))) {
    	
    		assertThat(app.getModel().getGroups(), equalTo(app.getGroupHelper().getUiGroups()));
    	}
    }*/
  }
}

